package com.ruoyi.purchase.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.AllocationQuantityParams;
import com.ruoyi.purchase.domain.OrderDetail;
import com.ruoyi.purchase.domain.RawProductDetail;
import com.ruoyi.purchase.service.IOrderDetailService;
import com.ruoyi.purchase.service.IRawProductDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * RawProductDetailController
 * 
 * @author kevinness
 * @date 2025-01-18
 */
@RestController
@RequestMapping("/RawProductDetail")
public class RawProductDetailController extends BaseController
{
    @Autowired
    private IRawProductDetailService rawProductDetailService;

    @Autowired
    private IOrderDetailService orderDetailService;

    /**
     * 查询RawProductDetail列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RawProductDetail rawProductDetail)
    {
        startPage();
        List<RawProductDetail> list = rawProductDetailService.selectRawProductDetailList(rawProductDetail);
        return getDataTable(list);
    }

    /**
     * 查询每个订单对应货品的剩余调拨数量
     */
    @PostMapping("/remain/allocation/quantity")
    public AjaxResult getRemainAllocationQuantityByOrderNo(@RequestBody String orderNo){
        List<OrderDetail> detailList =
                orderDetailService.selectOrderDetailByOrderNo(orderNo);
        List<AllocationQuantityParams> remainQuantity = new ArrayList<>();
        for (OrderDetail orderDetail : detailList) {
            AllocationQuantityParams allocationQuantityParams =
                    new AllocationQuantityParams(orderDetail);
            allocationQuantityParams.setRemainAllocationQuantity(rawProductDetailService.
                    getRemainAllocationQuantityByOrderDetail(orderDetail));
            remainQuantity.add(allocationQuantityParams);
        }
        return success(remainQuantity);
    }

    /**
     * 导出RawProductDetail列表
     */
    @Log(title = "RawProductDetail", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RawProductDetail rawProductDetail)
    {
        List<RawProductDetail> list = rawProductDetailService.selectRawProductDetailList(rawProductDetail);
        ExcelUtil<RawProductDetail> util = new ExcelUtil<RawProductDetail>(RawProductDetail.class);
        util.exportExcel(response, list, "RawProductDetail数据");
    }

    /**
     * 获取RawProductDetail详细信息
     */
    @GetMapping(value = "/{rawProductCode}")
    public AjaxResult getInfo(@PathVariable("rawProductCode") String rawProductCode)
    {
        return success(rawProductDetailService.selectRawProductDetailByRawProductCode(rawProductCode));
    }

    /**
     * 新增RawProductDetail
     */
    @Log(title = "RawProductDetail", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RawProductDetail rawProductDetail)
    {
        return toAjax(rawProductDetailService.insertRawProductDetail(rawProductDetail));
    }

    /**
     * 修改RawProductDetail
     */
    @Log(title = "RawProductDetail", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RawProductDetail rawProductDetail)
    {
        return toAjax(rawProductDetailService.updateRawProductDetail(rawProductDetail));
    }

    /**
     * 删除RawProductDetail
     */
    @Log(title = "RawProductDetail", businessType = BusinessType.DELETE)
    @DeleteMapping("/{rawProductCodes}")
    public AjaxResult remove(@PathVariable String[] rawProductCodes)
    {
        return toAjax(rawProductDetailService.deleteRawProductDetailByRawProductCodes(rawProductCodes));
    }

    /**
     * 根据订单编号获取所有货品的详细信息
     */
    @PostMapping("/list/orderNo")
    public AjaxResult getRawProductDetailListByOrderNo(@RequestBody String orderNo){
        return success(rawProductDetailService.selectRawProductDetailListByOrderNo(orderNo));
    }
}